/**
 * 设置界面
 */
package net.youpo.Market.UI;

import net.youpo.Market.R;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;

public class SettingActivity extends PreferenceActivity implements
		OnSharedPreferenceChangeListener {
	private ListPreference mInstallWhereLP;
	private ListPreference mDownloadItemsLP;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.settings);
		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(this);
		String installWhereValue = prefs.getString("installWhere", "手机");
		String downloadItemsValue = prefs.getString("downloadItems", "3");
		mInstallWhereLP = (ListPreference) findPreference("installWhere");
		mInstallWhereLP.setSummary(installWhereValue);
		mDownloadItemsLP = (ListPreference) findPreference("downloadItems");
		mDownloadItemsLP.setSummary(downloadItemsValue);
		prefs.registerOnSharedPreferenceChangeListener(this);
	}

	@Override
	public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
			Preference preference) {
		preference
				.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
					@Override
					public boolean onPreferenceChange(Preference preference,
							Object newValue) {

						String key = preference.getKey();
						// if (key.equals("editkey")) {
						// // 动态改变summary的值
						// if (((String) newValue).trim().equals(""))
						// preference.setSummary(R.string.xxxx);
						// else
						// preference.setSummary(getResources().getString(
						// R.string.xxxx)
						// + newValue);
						//
						// } else if (key.equals("listkey")) {
						// preference.setSummary(getResources().getString(
						// R.string.xxxx)
						// + newValue);
						// }
						return true;

					}

				});

		return true;
	}

	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
			String key) {
		if (key.equals("installWhere")) {
			mInstallWhereLP.setSummary(sharedPreferences.getString(key, ""));
		} else if (key.equals("downloadItems")) {
			mDownloadItemsLP.setSummary(sharedPreferences.getString(key, ""));
		}
	}
}
